<template>
  <div class="app-container">
    <Find
      @Launchform="Launchform5"
      @clickNumber="clickNumber"
      @clickNumber1="clickNumber1"
      :OriginalFilter="{ static: true }"
      :formList="formList"
      @findClicked="findData3"
    />

    <el-card style="margin-top: 10px" shadow="always" :body-style="{ padding: '5px' }">
      <h1>检核作业</h1>
      <Find
        @Launchform="Launchform"
        :OriginalFilter="{ static: true }"
        :searchImg="false"
        style="margin-bottom: 10px"
        :formList="formList1"
        @findClicked="findData1"
      />
      <div style="display: flex">
        <div style="width: 30%">
          <h1>材料入库查询</h1>
          <Table
            @selectList="selectList2"
            @currentRow="currentRow1"
            :isLoading="isLoading1"
            :selection="true"
            :tableHeader="tableHeader"
            :tableData="BottomWarehouseEntryNumberTableDate"
            :TableHeight="89"
          />
          <PageModule
            :PageData="{ buttonS: true, total: totalL }"
            :layout="'prev, pager'"
            @pageChange="pageChangeL"
            @pageSizeChange="pageSizeChangeL"
          />
        </div>
        <div style="width: 70%; margin-left: 10px">
          <div style="display: flex; justify-content: space-between">
            <h1>入库明细查询</h1>
            <div>
              <el-button :disabled="active"  style="margin-top: -10px; height: 25px" type="primary" size="default" @click="ChargingUnitPriceMethods">取单价</el-button>
              <el-button :disabled="active"  style="margin-top: -10px; height: 25px" type="primary" size="default" @click="TakeUnitPrice1">填充单价</el-button>
            </div>
          </div>
          <Table
            summary
            :summaryData="summaryData1"
            @inputChange="checkboxChange1"
            :isLoading="isLoading2"
            :selection="true"
            :tableHeader="tableHeader1"
            :tableData="InventoryDetailsQueryTableDate"
            :TableHeight="89"
          />
        </div>
      </div>

      <PageModule :PageData="{ pageSizeShow: true }" />
    </el-card>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { remarkQuery } from '@/api/GeneralFinanceDepartment/FactoryMaterialCost/SurfaceMaterialHarvestCheck'
import { ElMessage } from 'element-plus'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'ConsumablesHarvestInspection'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import type { formListType } from '@/components/Find/index.vue'
import { ref, onMounted } from 'vue'
import { VendorQuery } from '@/api/WarehouseDepartment/FactoryStorage/FitOutsourcingProcessing'
import {
  ChargingUnitPrice,
  BottomWarehouseEntryNumber,
  InventoryDetailsQuery,
  CheckWithCheckmark,
  preserve
} from '@/api/GeneralFinanceDepartment/FactoryMaterialCost/ConsumablesHarvestInspection'
import { vendorName } from '@/api/GeneralFinanceDepartment/FactoryMaterialCost/PackageMaterialHarvestCheck'
// 取单价
const ChargingUnitPriceMethods = async () => {
  try {
    const result = await ChargingUnitPrice(InventoryDetailsQueryTableDate.value)
    if (result.code === 200) {
      InventoryDetailsQueryMethods()
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('取单价失败请重试')
  }
}

const findData3 = (count: string) => {
  if (count === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
}

const Launchform5 = (row: any) => {
  BottomWarehouseEntryNumberParms.value.slipNo = row[0].inputValue
  BottomWarehouseEntryNumberParms.value.slipStartDate = row[1].dateValue[0]
  BottomWarehouseEntryNumberParms.value.slipEndDate = row[1].dateValue[1]
  BottomWarehouseEntryNumberParms.value.vendorName = row[2].selectValue.split(' ')[1]
  BottomWarehouseEntryNumberParms.value.inOutType = row[3].selectValue

  BottomWarehouseEntryNumberParms.value.lotNo = row[4].inputValue
  BottomWarehouseEntryNumberParms.value.sendNo = row[5].inputValue
  BottomWarehouseEntryNumberParms.value.sendMonth = row[6].inputValue
  BottomWarehouseEntryNumberMethdos()
}
const active = ref(false)
const clickNumber = (row: any) => {
  switch (row) {
    case '1': {
      BottomWarehouseEntryNumberParms.value.check = 1
      active.value = false
      formList1.value[0].disabled = false
      break
    }
    case '2': {
      BottomWarehouseEntryNumberParms.value.check = 2
      formList1.value[0].disabled = ''
      active.value = true
      break
    }
    case '3': {
      BottomWarehouseEntryNumberParms.value.check = ''
      formList1.value[0].disabled = false
      active.value = false
      break
    }
  }

  BottomWarehouseEntryNumberMethdos()
}

const clickNumber1 = (row: any) => {
  switch (row) {
    case '0': {
      BottomWarehouseEntryNumberParms.value.mark = ''
      break
    }
    case '1': {
      BottomWarehouseEntryNumberParms.value.mark = 1
      break
    }
    case '2': {
      BottomWarehouseEntryNumberParms.value.mark = 2
      break
    }
  }

  BottomWarehouseEntryNumberMethdos()
}

const TakeUnitPrice1 = () => {
  summaryData1.value[0].num = 0
  summaryData1.value[1].num = 0
  if (InventoryDetailsQueryTableDate.value[0].prices === null) {
    JMessage.warning('第一行单价不可为空')
  } else {
    InventoryDetailsQueryTableDate.value = InventoryDetailsQueryTableDate.value.map((item: any) => {
      item.prices = InventoryDetailsQueryTableDate.value[0].prices
      item.amts = item.prices * item.qty
      item.prices = parseFloat(item.prices).toFixed(5)
      item.amts = parseFloat(item.amts).toFixed(2)
      if (item.amts !== null) {
        summaryData1.value[1].num += parseFloat(item.amts)
      }
      if (item.qty !== null) {
        summaryData1.value[0].num += parseFloat(item.qty)
      }
      return item
    })
    summaryData1.value[0].num = parseFloat(summaryData1.value[0].num).toFixed(2)
    summaryData1.value[1].num = parseFloat(summaryData1.value[1].num).toFixed(2)
  }
}

const TakeUnitPrice = () => {
  summaryData1.value[0].num = 0
  summaryData1.value[1].num = 0

  InventoryDetailsQueryTableDate.value = InventoryDetailsQueryTableDate.value.map((item: any) => {
    if (item.amts !== null && item.qty !== null) {
      item.price = parseFloat((item.amt / item.qty).toFixed(4))
    } else {
      item.price = null
    }

    if (item.qty !== null) {
      summaryData1.value[0].num += parseFloat(item.qty)
    }
    if (item.amt !== null) {
      summaryData1.value[1].num += parseFloat(item.amt)
    }

    return item
  })
}

const summaryData1 = ref<any[]>([
  {
    index: 11,
    num: 0
  },
  {
    index: 14,
    num: 0
  }
])

const checkboxChange1 = async (list: any) => {
  if (list.prices !== null && list.qty !== null && list.prices !== '' && list.qty !== '') {
    list.amts = parseFloat((list.prices * list.qty).toFixed(4))
  }

  // if(list.price === ''){
  //   list.amt = null
  //   list.price = null
  // }
}

// 保存
const preserveMethods = async () => {
  try {
    const result = await preserve(
      InventoryDetailsQueryTableDate.value.map((item: any) => {
        if (item.ybFlag) {
          item.ybFlag = 'Y'
        }
        return item
      })
    )
    if (result.code === 200) {
      JMessage.success(result.message)
      InventoryDetailsQueryMethods()
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('保存出错请重试')
  }
}

// 打勾记录检核
const CheckWithCheckmarkParms = ref([])

const currentRow1 = (row: any) => {
  recQtyList.value = row.slipNo
  InventoryDetailsQueryMethods()
}

const choolseList = ref()

const RemarksConfirmationParms = ref([])

const selectList2 = (row: any) => {
  CheckWithCheckmarkParms.value = row.map((item: any) => {
    return item
  })

  RemarksConfirmationParms.value = row.map((item: any) => {
    return item
  })
}

const remark = ref('')

const Launchform = (row: any) => {
  remark.value = row[2].selectValue
}

const CheckWithCheckmarkMethods = async () => {
  try {
    const result = await CheckWithCheckmark(choolseList.value, CheckWithCheckmarkParms.value, remark.value)
    if (result.code === 200) {
      CheckWithCheckmarkParms.value = []
      JMessage.success(result.message)
      BottomWarehouseEntryNumberMethdos()
    } else {
      JMessage.error(result.message)
    }
  } catch (error) {
    JMessage.error('检核出错请重试')
  }
}

const findData1 = (row: any) => {
  if (row === '打勾记录检核') {
    choolseList.value = 1
    if (CheckWithCheckmarkParms.value.length === 0) {
      JMessage.warning('请勾选需要检核的数据')
    } else {
      CheckWithCheckmarkMethods()
    }
  }
  if (row === '打勾记录取消检核') {
    choolseList.value = 2
    if (CheckWithCheckmarkParms.value.length === 0) {
      JMessage.warning('请勾选需要取消检核的数据')
    } else {
      CheckWithCheckmarkMethods()
    }
  }

  if (row === '备注确认') {
    choolseList.value = 3
    if (RemarksConfirmationParms.value.length === 0) {
      JMessage.warning('请勾选需要修改备注的数据')
    } else if (remark.value == '') {
      JMessage.warning('请选择备注信息')
    } else {
      CheckWithCheckmarkMethods()
    }
  }
  if (row === '保存') {
    preserveMethods()
  }
}

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '入库单号',
    inputValue: ''
  },
  {
    type: 'date',
    dateLabel: '入库日期',
    dateValue: ''
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '厂商',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'input',
    inputPlaceholder: '指令',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '材料名称',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '送货单号',
    inputValue: ''
  },
  {
    type: 'input',
    inputPlaceholder: '送货月份',
    inputValue: ''
  },
  {
    type: 'radio',
    checkboxLabel: '未检核',
    checked: false,
    label: '1'
  },
  {
    type: 'radio',
    checkboxLabel: '已检核',
    checked: false,
    label: '2'
  },
  {
    type: 'radio',
    checkboxLabel: '全部',
    checked: true,
    label: '3'
  },
  {
    type: 'radio1',
    checkboxLabel: '未标记',
    checked: false,
    label: '1'
  },
  {
    type: 'radio1',
    checkboxLabel: '已标记',
    checked: false,
    label: '2'
  },
  {
    type: 'radio1',
    checkboxLabel: '全部',
    checked: true,
    label: '0'
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '明细查询'
  }
]) as any

const formList1 = ref<any[]>([
  {
    type: 'button',
    checkboxLabel: '打勾记录检核'
  },
  {
    type: 'button',
    checkboxLabel: '打勾记录取消检核'
  },
  {
    type: 'select',
    selectValue: '',
    selectP: '备注信息',
    selectLabel: '',
    selectOptions: []
  },
  {
    type: 'button',
    checkboxLabel: '备注确认'
  },
  {
    type: 'button',
    checkboxLabel: '保存'
  }
])

// 材料入库查询

const InventoryDetailsQueryTableDate = ref<any>([])

const recQtyList = ref('')

const isLoading2 = ref(true)

const InventoryDetailsQueryMethods = async () => {
  summaryData1.value[0].num = 0
  summaryData1.value[1].num = 0
  try {
    const result = await InventoryDetailsQuery(recQtyList.value)
    InventoryDetailsQueryTableDate.value = result.map((item: any) => {
      if (item.cfmFlag !== 'Y') {
        item.change = 'Y'
        item.disableConfirm = true
      }
      if (item.ybFlag === 'Y') {
        item.ybFlag = true
        item.backgroundColor = '#FFE0E0'
      }

      if (item.prices === 0) {
        item.price = null
      }

      if (item.amts === 0) {
        item.amt = null
      }

      if (item.qty !== null) {
        summaryData1.value[0].num += parseFloat(item.qty)
      }

      if (item.amts !== null) {
        summaryData1.value[1].num += parseFloat(item.amts)
      }

      return item
    })
    summaryData1.value[0].num = parseFloat(summaryData1.value[0].num).toFixed(2)
    summaryData1.value[1].num = parseFloat(summaryData1.value[1].num).toFixed(2)

    isLoading2.value = false
  } catch (error) {
    JMessage.error('入库明细查询出错')
    isLoading2.value = false
  }
}

// 厂商查询
const VendorQueryMethods = async () => {
  let NewArray = []
  const result = await vendorName()
  NewArray = result.reduce((arr: any, item: any) => {
    if (item.vendorNo && item.vendorName) {
      // 只添加非空的 typeName 和 typeNo
      arr.push(item.vendorNo + ' ' + item.vendorName)
    }
    return arr
  }, [])
  formList.value[2].selectOptions = EvaluatesTheArrayReturnValue(NewArray)
}

const EvaluatesTheArrayReturnValue = (ArrayList: any) => {
  return ArrayList.map((item: any, index: any) => {
    return {
      optionKey: index,
      optionValue: item,
      optionLabel: item
    }
  })
}

// 备注信息查询
const remarkQueryMethods = async () => {
  try {
    const result = await remarkQuery()
    formList1.value[2].selectOptions = EvaluatesTheArrayReturnValue(result)
  } catch (error) {
    JMessage.error('备注信息查询出错')
  }
}

// 大底入库单查询
const BottomWarehouseEntryNumberTableDate = ref<any>([])

const isLoading1 = ref(true)

// 单号查询
const BottomWarehouseEntryNumberParms = ref<any>({
  check: '1',
  inOutType: '',
  lotNo: '',
  mark: '',
  pageNum: 1,
  pageSize: 40,
  sendNo: '',
  slipEndDate: '',
  slipNo: '',
  slipStartDate: '',
  vendorName: ''
})

const BottomWarehouseEntryNumberMethdos = async () => {
  try {
    isLoading1.value = true
    const result = await BottomWarehouseEntryNumber(BottomWarehouseEntryNumberParms.value)
    BottomWarehouseEntryNumberTableDate.value = result.list.map((item: any) => {
      if (item.cfmFlag === 'Y') {
        item.backgroundColor = '#80c080'
      }

      return item
    })
    totalL.value = result.total
    if (result.list.length !== 0) {
      recQtyList.value = result.list[0].slipNo
      InventoryDetailsQueryMethods()
    }
    isLoading1.value = false
  } catch (error) {
    JMessage.error('大底入库单号查询出错')
    isLoading1.value = false
  }
}

const totalL = ref(0)

const pageChangeL = (val: any) => {
  BottomWarehouseEntryNumberParms.value.pageNum = val
  BottomWarehouseEntryNumberMethdos()
}

const pageSizeChangeL = (val: any) => {
  BottomWarehouseEntryNumberParms.value.pageSize = val
  BottomWarehouseEntryNumberMethdos()
}

onMounted(() => {
  VendorQueryMethods()

  remarkQueryMethods()
})

// 表格数据

const tableHeader = ref([
  {
    label: '入库单号',
    prop: 'slipNo',
    backgroundColor: true
  },
  {
    label: '入库日期',
    prop: 'slipDate'
  },
  {
    label: '标记',
    prop: 'recFlag',
    width: '50'
  },
  {
    label: '财务备注',
    prop: 'remark'
  }
])

const tableHeader1 = ref([
  {
    label: '送货日期',
    prop: 'sysDate',
    change: true
  },
  {
    label: '送货单号',
    prop: 'sendNo',
    change: true
  },
  {
    label: '订购单号',
    prop: 'purNo',
    change: true
  },
  {
    label: '厂商名称',
    prop: 'vendorName',
    change: true
  },
  {
    label: '指令',
    prop: 'lotNo',
    change: true
  },
  {
    label: '型体',
    prop: 'style',
    change: true
  },
  {
    label: '材料颜色',
    prop: 'mtlColor',
    change: true
  },
  {
    label: '材料名称',
    prop: 'mtlName',
    change: true
  },
  {
    label: '规格',
    prop: 'mtlSpec',
    change: true
  },
  {
    label: '单位',
    prop: 'mtlUnit',
    change: true
  },

  {
    label: '入库数量',
    prop: 'qty',
    change: true
  },
  {
    label: '类型',
    prop: 'slipType',
    change: true
  },
  {
    label: '单价',
    prop: 'prices'
  },
  {
    label: '金额',
    prop: 'amts'
  },
  {
    label: '财务备注',
    prop: 'financeMemo'
  },
  {
    label: '仓管备注',
    prop: 'remark',
    change: true
  },
  {
    label: '仓管员',
    prop: 'sysUser',
    change: true
  }
])
</script>

<style lang="less" scoped>
h1 {
  font-size: 14px;
  margin-bottom: 5px;
}
</style>
